﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MShop.Core.Domain;
using MShop.Core.DataAccess;
using MShop.Web.Components.Paging;
using MShop.Core.Extensions;
using MShop.Web.Components.Extensions;
using MShop.Core;


namespace MShop.Web.Areas.Admin.Controllers
{
    public class TradeController : SecureController
    {
        private readonly IItemCatDao _ItemCatDao;
        private readonly ITradeDao _TradeDao;
        public TradeController(IItemCatDao itemcatdao, ITradeDao tradeDao)
        {
            _TradeDao = tradeDao;
            _ItemCatDao = itemcatdao;
        }

        public ActionResult Index(int? page, string key, int? status)
        {
            int pageSize = 20;
            int pageIndex = page.HasValue ? page.Value - 1 : 0;

            IQueryable<Trade> q = _TradeDao.GetAll();
            if (!string.IsNullOrEmpty(key))
            {
                q = from item in q where item.TradeId.Contains(key) || item.ShopUsers.aspnet_Users.UserName.Contains(key) select item;
            }
            if (status.HasValue)
            {
                if (status.Value != -1)
                {
                    q = from item in q where item.TradeStatus == status.Value  select item;
                }
            }
            int count = q.Count();
            q = q.OrderByDescending(x => x.Created);
            q = q.Skip(pageIndex * pageSize).Take(pageSize);
            PagedList<Trade> pagedlist = new PagedList<Trade>(q, pageIndex, pageSize, count);
            var zeroListItem = new SelectListItem() { Text = "所有", Value = "-1" };

            ViewData["status"] = typeof(TradeStatus).ConvertListItem(status, zeroListItem);

            return View(pagedlist);
        }


        public ActionResult Edit(string id)
        {
            var model = _TradeDao.FirstOrDefault(x => x.TradeId == id);
            ViewData["status"] = typeof(TradeStatus).ConvertListItem(model.TradeStatus);
            return View(model);            
        }


        public ActionResult Delete(string id)
        {
            var trade = _TradeDao.FirstOrDefault(x => x.TradeId == id);           
            _TradeDao.Delete(trade);
            _TradeDao.SaveChanges();
            return RedirectToAction("index");
        }


        [HttpPost]
        public ActionResult Edit(string id, Trade trade)
        {
            ViewData["status"] = typeof(TradeStatus).ConvertListItem(trade.TradeStatus);

            var dbTrade = _TradeDao.FirstOrDefault(x => x.TradeId == id);

            dbTrade.Modified = DateTime.Now;          
       
            dbTrade.TradeStatus = trade.TradeStatus;
            dbTrade.PostFee = trade.PostFee;
            dbTrade.SellerMemo = trade.SellerMemo;
            dbTrade.DisCount = trade.DisCount;
          
            dbTrade.TotalPoint = trade.TotalPoint;

            _TradeDao.SaveChanges();
            return RedirectToAction("index");

        }
    }
}
